<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use App\Models\ReplyComment;
use App\Models\Subtask;
use App\Models\Task;
use App\Models\WeChatAccount;
use App\Models\Video;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\Request;

class TaskController extends Controller
{
    use ApiResponseTrait;


    //获取视频列表
    public function getVideoList(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);

        $videos = Video::with('category')
            ->forPage($page, $limit)
            ->get();


        return $this->success('success', $videos);
    }

    //获取视频详情
    public function getVideo(Request $request){
        $videoId = $request->input('id');

        if ($videoId) {
            $video = Video::with('category')->find($videoId);
            if (!$video) {
                return $this->error('视频不存在');
            }
        } else {
            // 随机返回一个视频
            $video = Video::with('category')->inRandomOrder()->first();
            if (!$video) {
                return $this->error('暂无视频');
            }
        }

        // 增加浏览次数
        $video->increment('browse_num');

        return $this->success('success', $video);
    }

    public function getTask(Request $request)
    {
        try {
            $account_num = $request->input('account','');
            $account = WeChatAccount::where('account', $account_num)->first();
            if(!$account){
                throw new \Exception('账号不存在');
            }
            $subtask = Subtask::where('account_id',$account->id)->where('status',1)->first();
            if($subtask){
                $data = [
                    'task_type' => 2,
                    'subtask' => $subtask,
                ];
            }else{
                $task = Task::where('account_id',$account->id)->where('status',1)->first();
                if($task){
                    $data = [
                        'task_type' => 1,
                        'task' => $task,
                    ];
                }else{
                    $data = [
                        'task_type' => 0,
                        'task' => [],
                        'subtask' => [],
                    ];
                }


            }




        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('success',$data);
    }

    public function saveSubtask(Request $request)
    {

        try {
            $account_num = $request->input('account','');
            $title = $request->input('title','');
            $time = $request->input('time','');
            $nickname = $request->input('nickname','');
            $task_id = $request->input('task_id','');
            if(empty($title)){
                throw new \Exception('标题不能为空');
            }
            $md5_title =  strtolower(md5($title));

            $account = WeChatAccount::where('account', $account_num)->first();
            if(!$account){
                throw new \Exception('账号不存在');
            }
            $task = Task::where('id',$task_id)
                ->where('account_id',$account->id)
                ->where('status',1)->first();

            if(empty($task)){
                throw new \Exception('任务不存在,无法写入');
            }

            $sub_task = Subtask::where('account_id',$account->id)->where('task_id',$task_id)->where('md5_title',$md5_title)->first();
            if(!$sub_task){
                $sub_task = new Subtask();
                $sub_task->status = 1;
                $sub_task->title = $title;
                $sub_task->md5_title = $md5_title;
                $sub_task->nickname = $nickname;
                $sub_task->account_id = $account->id;
                $sub_task->task_id = $task->id;
                $sub_task->time = $time;
                $sub_task->save();
            }
            $is_run = 0;
            if($sub_task->status == 1){
                $is_run = 1;
            }
            $data['is_run'] = $is_run;

            return $this->success('写入成功',$data);

        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }


    }

    public function saveComment(Request $request){
        try {
            $account_num = $request->input('account','');
            $comment = $request->input('comment','');
            $time = $request->input('time','');
            $nickname = $request->input('nickname','');
            $subtask_id = $request->input('subtask_id','');
            $place = $request->input('place','');
            $is_video = $request->input('is_video',0);
            if(empty($comment)){
                throw new \Exception('评论内容不能为空');
            }

            $account = WeChatAccount::where('account', $account_num)->first();
            if(!$account){
                throw new \Exception('账号不存在');
            }
            $sub_task = Subtask::where('id',$subtask_id)
                ->where('account_id',$account->id)
                ->where('status',1)->first();
            $match_name = $sub_task->task->match_name;
            if(empty($sub_task)){
                throw new \Exception('子任务不存在,无法写入');
            }
            $Comment = Comment::where('subtask_id',$sub_task->id)
                ->where('comment',$comment)
                ->first();
            if(empty($Comment)){
                $Comment = new Comment();
                $Comment->comment = $comment;
                $Comment->nickname = $nickname;
                $Comment->subtask_id = $sub_task->id;
                $Comment->task_id = $sub_task->task->id;
                $Comment->time = $time;
                $Comment->place = $place;
                $Comment->is_video = $is_video;
                $Comment->save();
            }


            if(strpos($comment, $match_name) !== false ){
                $reply_content = Comment::getRandomValue($sub_task->task->replys);
                $data = [
                    'is_send' => 1,
                    'comment_id' => $Comment->id,
                    'reply_content' => $reply_content,
                ];
            }else{
                $data = [
                    'is_send' => 0,
                    'reply_content' => '',
                ];
            }


        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('写入成功',$data);
    }

    public function replyContent(Request $request)
    {
        try {
            $account_num = $request->input('account','');
            $reply_content = $request->input('reply_content','');
            $is_successful = $request->input('is_successful','');
            $comment_id = $request->input('comment_id','');
            $error_msg = $request->input('error_msg','');

            $account = WeChatAccount::where('account', $account_num)->first();
            if(!$account){
                throw new \Exception('账号不存在');
            }

            $comment = Comment::where('id',$comment_id)->first();
            if(empty($comment)){
                throw new \Exception('评论不存在');
            }

            $replyComment = new ReplyComment();
            $replyComment->account_id = $account->id;
            $replyComment->comment_id = $comment->id;
            $replyComment->reply_content = $reply_content;
            $replyComment->is_successful = $is_successful;
            $replyComment->error_msg = $error_msg;
            $replyComment->save();






        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('success');
    }

    public function endSubtask(Request $request)
    {
        try{
            $account_num = $request->input('account','');
            $subtask_id = $request->input('subtask_id','');
            $account = WeChatAccount::where('account', $account_num)->first();
            if(!$account){
                throw new \Exception('账号不存在');
            }

            $subtask = Subtask::where('id',$subtask_id)
                ->where('account_id',$account->id)
                ->where('status',1)
                ->first();
            if(empty($subtask)){
                throw new \Exception('子任务不存在');
            }
            $subtask->status = 3;
            $subtask->save();
            $count_num = Subtask::where('task_id',$subtask->task_id)
                ->where('account_id',$account->id)
                ->where('status',1)
                ->count();
            if($count_num == 0){
                Task::where('id',$subtask->task_id)->update(['status'=>3]);
            }



        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        return $this->success('success');

    }
}
